<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IBranchAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] .
		'/SAGER/FirePHPCore-0.3.2/lib/FirePHPCore/FirePHP.class.php';
		
	$debug = FirePHP :: getInstance(true);
	$debug -> setEnabled(true);
	
	error_reporting (E_ALL ^ E_NOTICE);

 	class MySqlBranchAccessDao implements IBranchAccessDao {  
 		/**
		 * Constructor of the branch dao
		 */
		public function __construct () {}
	 
  
		public function createBranch  ($pk_rif,$name) {
			$connection = ConnectionFactory::getMySqlConnectionAccessDao();
			$conn=$connection->openConnection();
			//$conn = mysql_connect("localhost", "root", "");
			//mysql_select_db("sager", $conn);			 
			$query = "INSERT INTO BRANCH VALUES ('$pk_rif','$name')";
			
			$sentence=mysql_query($query,$conn);
			if ($sentence)
				return true;
			
			return false;
		 
		 }
		 
		 public function changeBranch  ($pkRif,$name)
		 {
			$connection = ConnectionFactory::getMySqlConnectionAccessDao();
			$conn=$connection->openConnection();
			$conn = mysql_connect("localhost", "root", "");
			mysql_select_db("sager", $conn);
			
			$query = "UPDATE BRANCH SET pk_rif='$pk_rif',name='$name'";
		 
		 
		 
			$sentence=mysql_query($query,$conn);
			   if ($sentence){
					return true;
				   }
			  return false;
		 
		 }
		 
		/**
		 * Selects all branches
		 */
		public function selectBranches() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
				$GLOBALS['debug']->log('INICIO Funcion DAO las sucursales');
			$query = "SELECT b.pk_rif, b.name, CONCAT( COUNTRY.name, ', ', STATE.name, ', ', CITY.name, ', ', ZONE.name ) address
					  FROM BRANCH b, BRANCH_ADDRESS ba, PLACE COUNTRY, PLACE STATE, PLACE CITY, PLACE ZONE
					  WHERE b.pk_rif = ba.pk_fk_rif_branch
                      AND ba.pk_fk_id_place = ZONE.pk_id
                      AND ZONE.fk_id_place = CITY.pk_id
					  AND CITY.fk_id_place = STATE.pk_id
					  AND STATE.fk_id_place = COUNTRY.pk_id";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			$GLOBALS['debug']->log(' FIN Funcion DAO las sucursales');	
			return $sentence;
		}
		
		 
		 /**
		  * Selects load branch
		  */
		 public function selectLoadBranch() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT pk_rif, name FROM BRANCH";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		 }
		 
		
		
	}
?>